/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.entities.CvEvaluationvehicule;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvTypeevaluationvehicule;
import fr.cggveritas.crewintranet.jms.entities.CvVehicule;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvEvaluationvehiculeJpaController implements Serializable {

    public CvEvaluationvehiculeJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CvEvaluationvehicule cvEvaluationvehicule) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypeevaluationvehicule cvTypeevaluationvehiculeIdtypeevaluation = cvEvaluationvehicule.getCvTypeevaluationvehiculeIdtypeevaluation();
            if (cvTypeevaluationvehiculeIdtypeevaluation != null) {
                cvTypeevaluationvehiculeIdtypeevaluation = em.getReference(cvTypeevaluationvehiculeIdtypeevaluation.getClass(), cvTypeevaluationvehiculeIdtypeevaluation.getIdtypeevaluation());
                cvEvaluationvehicule.setCvTypeevaluationvehiculeIdtypeevaluation(cvTypeevaluationvehiculeIdtypeevaluation);
            }
            CvVehicule cvVehiculeIdvehicule = cvEvaluationvehicule.getCvVehiculeIdvehicule();
            if (cvVehiculeIdvehicule != null) {
                cvVehiculeIdvehicule = em.getReference(cvVehiculeIdvehicule.getClass(), cvVehiculeIdvehicule.getIdvehicule());
                cvEvaluationvehicule.setCvVehiculeIdvehicule(cvVehiculeIdvehicule);
            }
            em.persist(cvEvaluationvehicule);
            if (cvTypeevaluationvehiculeIdtypeevaluation != null) {
                cvTypeevaluationvehiculeIdtypeevaluation.getCvEvaluationvehiculeList().add(cvEvaluationvehicule);
                cvTypeevaluationvehiculeIdtypeevaluation = em.merge(cvTypeevaluationvehiculeIdtypeevaluation);
            }
            if (cvVehiculeIdvehicule != null) {
                cvVehiculeIdvehicule.getCvEvaluationvehiculeList().add(cvEvaluationvehicule);
                cvVehiculeIdvehicule = em.merge(cvVehiculeIdvehicule);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvEvaluationvehicule cvEvaluationvehicule) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvEvaluationvehicule persistentCvEvaluationvehicule = em.find(CvEvaluationvehicule.class, cvEvaluationvehicule.getIdevaluationvehicule());
            CvTypeevaluationvehicule cvTypeevaluationvehiculeIdtypeevaluationOld = persistentCvEvaluationvehicule.getCvTypeevaluationvehiculeIdtypeevaluation();
            CvTypeevaluationvehicule cvTypeevaluationvehiculeIdtypeevaluationNew = cvEvaluationvehicule.getCvTypeevaluationvehiculeIdtypeevaluation();
            CvVehicule cvVehiculeIdvehiculeOld = persistentCvEvaluationvehicule.getCvVehiculeIdvehicule();
            CvVehicule cvVehiculeIdvehiculeNew = cvEvaluationvehicule.getCvVehiculeIdvehicule();
            if (cvTypeevaluationvehiculeIdtypeevaluationNew != null) {
                cvTypeevaluationvehiculeIdtypeevaluationNew = em.getReference(cvTypeevaluationvehiculeIdtypeevaluationNew.getClass(), cvTypeevaluationvehiculeIdtypeevaluationNew.getIdtypeevaluation());
                cvEvaluationvehicule.setCvTypeevaluationvehiculeIdtypeevaluation(cvTypeevaluationvehiculeIdtypeevaluationNew);
            }
            if (cvVehiculeIdvehiculeNew != null) {
                cvVehiculeIdvehiculeNew = em.getReference(cvVehiculeIdvehiculeNew.getClass(), cvVehiculeIdvehiculeNew.getIdvehicule());
                cvEvaluationvehicule.setCvVehiculeIdvehicule(cvVehiculeIdvehiculeNew);
            }
            cvEvaluationvehicule = em.merge(cvEvaluationvehicule);
            if (cvTypeevaluationvehiculeIdtypeevaluationOld != null && !cvTypeevaluationvehiculeIdtypeevaluationOld.equals(cvTypeevaluationvehiculeIdtypeevaluationNew)) {
                cvTypeevaluationvehiculeIdtypeevaluationOld.getCvEvaluationvehiculeList().remove(cvEvaluationvehicule);
                cvTypeevaluationvehiculeIdtypeevaluationOld = em.merge(cvTypeevaluationvehiculeIdtypeevaluationOld);
            }
            if (cvTypeevaluationvehiculeIdtypeevaluationNew != null && !cvTypeevaluationvehiculeIdtypeevaluationNew.equals(cvTypeevaluationvehiculeIdtypeevaluationOld)) {
                cvTypeevaluationvehiculeIdtypeevaluationNew.getCvEvaluationvehiculeList().add(cvEvaluationvehicule);
                cvTypeevaluationvehiculeIdtypeevaluationNew = em.merge(cvTypeevaluationvehiculeIdtypeevaluationNew);
            }
            if (cvVehiculeIdvehiculeOld != null && !cvVehiculeIdvehiculeOld.equals(cvVehiculeIdvehiculeNew)) {
                cvVehiculeIdvehiculeOld.getCvEvaluationvehiculeList().remove(cvEvaluationvehicule);
                cvVehiculeIdvehiculeOld = em.merge(cvVehiculeIdvehiculeOld);
            }
            if (cvVehiculeIdvehiculeNew != null && !cvVehiculeIdvehiculeNew.equals(cvVehiculeIdvehiculeOld)) {
                cvVehiculeIdvehiculeNew.getCvEvaluationvehiculeList().add(cvEvaluationvehicule);
                cvVehiculeIdvehiculeNew = em.merge(cvVehiculeIdvehiculeNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cvEvaluationvehicule.getIdevaluationvehicule();
                if (findCvEvaluationvehicule(id) == null) {
                    throw new NonexistentEntityException("The cvEvaluationvehicule with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvEvaluationvehicule cvEvaluationvehicule;
            try {
                cvEvaluationvehicule = em.getReference(CvEvaluationvehicule.class, id);
                cvEvaluationvehicule.getIdevaluationvehicule();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvEvaluationvehicule with id " + id + " no longer exists.", enfe);
            }
            CvTypeevaluationvehicule cvTypeevaluationvehiculeIdtypeevaluation = cvEvaluationvehicule.getCvTypeevaluationvehiculeIdtypeevaluation();
            if (cvTypeevaluationvehiculeIdtypeevaluation != null) {
                cvTypeevaluationvehiculeIdtypeevaluation.getCvEvaluationvehiculeList().remove(cvEvaluationvehicule);
                cvTypeevaluationvehiculeIdtypeevaluation = em.merge(cvTypeevaluationvehiculeIdtypeevaluation);
            }
            CvVehicule cvVehiculeIdvehicule = cvEvaluationvehicule.getCvVehiculeIdvehicule();
            if (cvVehiculeIdvehicule != null) {
                cvVehiculeIdvehicule.getCvEvaluationvehiculeList().remove(cvEvaluationvehicule);
                cvVehiculeIdvehicule = em.merge(cvVehiculeIdvehicule);
            }
            em.remove(cvEvaluationvehicule);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvEvaluationvehicule> findCvEvaluationvehiculeEntities() {
        return findCvEvaluationvehiculeEntities(true, -1, -1);
    }

    public List<CvEvaluationvehicule> findCvEvaluationvehiculeEntities(int maxResults, int firstResult) {
        return findCvEvaluationvehiculeEntities(false, maxResults, firstResult);
    }

    private List<CvEvaluationvehicule> findCvEvaluationvehiculeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvEvaluationvehicule.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvEvaluationvehicule findCvEvaluationvehicule(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvEvaluationvehicule.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvEvaluationvehiculeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvEvaluationvehicule> rt = cq.from(CvEvaluationvehicule.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
